#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringList>
#include "inputdelegate.h"
#include "buttondelegate.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setRowCount(2);

    QStringList _header;
    _header <<"Devices"<<"Input"<<"Control"<<"Status"<<"ttt";
    ui->tableWidget->setHorizontalHeaderLabels(_header);
    ui->tableWidget->setItemDelegateForColumn(1, new InputDelegate(this));
    ui->tableWidget->setItemDelegateForColumn(2, new ButtonDelegate(this));
//    ui->tableWidget->setColumnWidth(3, 80);
    // first
    QTableWidgetItem *item0 = new QTableWidgetItem("Device 1sdjfisdjfoejaifosdjfie");
    item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(0, 0, item0);

    // second column (dropbown)
    QTableWidgetItem *item1 = new QTableWidgetItem("DVI");
    item1->setFlags(item1->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(0, 1, item1);
    ui->tableWidget->openPersistentEditor((item1)); // always show dropdown

    // third colum (button)
    QTableWidgetItem *item2 = new QTableWidgetItem();
    item2->setFlags(item2->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(0, 2, item2);
    ui->tableWidget->openPersistentEditor((item2));

    // fourth column
    QTableWidgetItem *item3 = new QTableWidgetItem("djsifjsdifjeaijfdsifjedsfsdfaesdffffffffffffffffeadsfaeaia");
    item3->setSizeHint(QSize(120, 40));
    item3->setFlags(item3->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(0, 3, item3);

    QTableWidgetItem *item4 = new QTableWidgetItem("djsifjsdifjeaijfdsifjeia");
    item4->setFlags(item4->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(0, 4, item4);
//    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    ///////////////////////////////
    // first
    QTableWidgetItem *item10 = new QTableWidgetItem("Device 1");
    item10->setFlags(item10->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(1, 0, item10);

    // second column (dropbown)
    QTableWidgetItem *item11 = new QTableWidgetItem("DVI");
    item11->setFlags(item11->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(1, 1, item11);
    ui->tableWidget->openPersistentEditor((item11)); // always show dropdown

    // third colum (button)
    QTableWidgetItem *item12 = new QTableWidgetItem();
    item12->setFlags(item12->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(1, 2, item12);
    ui->tableWidget->openPersistentEditor((item12));

    // fourth column
    QTableWidgetItem *item13 = new QTableWidgetItem("djsidsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
//    item13->setSizeHint(QSize(120, 40));
    item13->setFlags(item13->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(1, 3, item13);

    QTableWidgetItem *item14 = new QTableWidgetItem("djsifjsdifjeaijfdsifjeia");
    item14->setFlags(item14->flags() & ~Qt::ItemIsEditable);
    ui->tableWidget->setItem(1, 4, item14);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    ui->tableWidget->resizeColumnToContents(0);
    ui->tableWidget->resizeColumnToContents(3);



    connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)),
                this, SLOT(changeItem()));


}

void MainWindow::changeItem()
{
    qDebug() << "Item changed.";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(this->ui->pushButton->isChecked())
    {
        this->ui->tableWidget->setColumnHidden(4, true);
    }
    else
    {
        this->ui->tableWidget->setColumnHidden(4, false);
        this->ui->tableWidget->resizeColumnToContents(3);
    }
}
